package com.papper.common.service.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.papper.common.bean.sys.SysPageConfig;
import com.papper.common.mapper.PapperMapper;
import com.papper.common.mapper.sys.SysPageConfigMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:LaJiao
 * @Date:2019-07-17 15:27:38
 * @Description:系统页面配置
 */
@Service("sysPageConfigService")
public class SysPageConfigService extends ServiceImpl<SysPageConfigMapper, SysPageConfig> {

	@Override
	protected void clearCaches(SysPageConfig entity) {
		super.clearCaches(entity);

	}

	public SysPageConfig findByPaygeCode(final String sysCode, final String userId, final String pageCode) {
		if (StringUtil.isEmpty(sysCode) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(pageCode)) {
			return null;
		}
		Example example = new Example(SysPageConfig.class);
		example.createCriteria().andEqualTo("systemCode", sysCode).andEqualTo("userId", userId).andEqualTo("pageCode", pageCode).andEqualTo("limit", 1);
		return findOne(example);
	}

	public String findShowItem(String sysId, String userid, String pageCode) {
		SysPageConfig entity = findByPaygeCode(sysId, userid, pageCode);
		List<String>  rtn    = Lists.newArrayList();
		if (entity != null) {
			JSONObject pageConfig = JSON.parseObject(entity.getPageConfig());
			for (String k : pageConfig.keySet()) {
				if ("1".equals(pageConfig.getString(k))) {
					rtn.add(k);
				}
			}
		}
		return StringTool.ListStringToString(rtn, ",");
	}
}